//对于axios的二次封装
import axios from "axios"
import nprogress from "nprogress"
import "nprogress/nprogress.css"
import store from "@/store"

//利用axios对象的方法create,创建axios实例
const requests = axios.create({
    //基础路径，发送请求时路径会自动添加/api
    baseURL: '/api',
    //请求超时时间5s
    timeout: 5000
})

//请求发送之前的处理的事务
requests.interceptors.request.use(
    (config) => {
        if(store.state.detail.uuid_token){
            config.headers.userTempId = store.state.detail.uuid_token
        }
        if (store.state.user.token) {
            config.headers.token = store.state.user.token;
        }
        nprogress.start();
        
        return config
    }),
    (error) => {
        return Promise.reject(new Error('faile'))
    }

//响应结果接受之前的处理的事务
requests.interceptors.response.use(
    (res) => {
        nprogress.done();
        return res.data
    }),
    (error) => {
        return Promise.reject(new Error('faile'))
    }

export default requests